﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ISIM
{
    public partial class MainForm : Form
    {
        public SkypeClass skype;
        public XmppClass fb;
        public XmppClass gt;
        public IcqClass icq;

        public Thread skypeThread;
        public Thread facebookThread;
        public Thread googleThread;
        public Thread icqThread;

        System.Windows.Forms.Timer checkConnectionTimer = new System.Windows.Forms.Timer();
        System.Windows.Forms.Timer availabilityTimer = new System.Windows.Forms.Timer();

        public MainForm()
        {
            InitializeComponent();

            Global.ISIMFunctions.Migrate();

            this.Text = Global.appName;
            this.notifyIcon.Text = Global.appName;

            TextEdit.InitializeEmoticons();
            Global.ISIMFunctions.LoadLogin();

            TranslateForm();

            // musím si to nahrát do proměnné, bže ten slovník modifikuju, by hodilo exception
            string[] settings = Global.settingsDictionary.Keys.ToArray();
            foreach (string list in settings)
                Global.settingsDictionary[list] = Global.ISIMFunctions.LoadISIMsettingsFile(list);

            settings = Global.userSpecialList.Keys.ToArray();
            foreach (string list in settings)
                Global.userSpecialList[list] = Global.ISIMFunctions.LoadISIMsettingsListFile(list);


            if ((Global.settingsDictionary["ISIMsettings"].ContainsKey("myNickTextBox")) && (Global.settingsDictionary["ISIMsettings"]["myNickTextBox"].Replace(" ", "").Length > 0))
                Global.myNick = Global.settingsDictionary["ISIMsettings"]["myNickTextBox"];
            else
                Global.myNick = Global.LangManager.Translate("me");

            if (Global.settingsDictionary["ISIMsettings"].ContainsKey("chatFont"))
            {
                string[] font = Global.settingsDictionary["ISIMsettings"]["chatFont"].Split('|');
                if (font.Length == 3)
                    Global.chatFont = new Font(font[0], float.Parse(font[1]), (FontStyle)Enum.Parse(typeof(FontStyle), font[2]));
            }

            contactListView.ListViewItemSorter = new ISIMcontactComparer();

            checkConnectionTimer.Interval = 30 * 1000;
            checkConnectionTimer.Tick += new EventHandler(checkConnectionTimer_Tick);

            availabilityTimer.Interval = 5 * 1000;
            availabilityTimer.Tick += availabilityTimer_Tick;
        }

        public void TranslateForm()
        {
            contactListView.Groups["favouriteListViewGroup"].Header = Global.LangManager.Translate("favourite");

            mainMenuItem.Text = Global.LangManager.Translate("main");
                preferencesItem.Text = Global.LangManager.Translate("preferences");
                protocolsLogin.Text = Global.LangManager.Translate("protocols");
                newSkypeContact.Text = Global.LangManager.Translate("newSkypeContact");
                skypeAccount.Text = Global.LangManager.Translate("skypeProfileSettings");
                closeISIM.Text = Global.LangManager.Translate("exit");

            quickMenu.Text = Global.LangManager.Translate("quick");
                muteMic.Text = Global.LangManager.Translate("muteMic");
                topMost.Text = Global.LangManager.Translate("topMost");

            helpMenu.Text = Global.LangManager.Translate("help");
                aboutISIM.Text = Global.LangManager.Translate("aboutISIM");
                checkForUpdates.Text = Global.LangManager.Translate("checkForUpdates");

            showGuiItem.Text = Global.LangManager.Translate("show");
            closeISIMItem.Text = Global.LangManager.Translate("exit");

            renameContact.Text = Global.LangManager.Translate("rename");
            remindWhenAvailable.Text = Global.LangManager.Translate("remindWhenAvailable");
            favouriteContact.Text = Global.LangManager.Translate("favourite");
            removeContact.Text = Global.LangManager.Translate("remove");
        }

        /// <summary>
        /// Otevření okna aplikace pokud je již spuštěná a snaží se spustit další isntanci
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == Global.SingleInstance.WM_SHOWME)
                this.ShowForm();

            base.WndProc(ref m);
        }

        /// <summary>
        /// Zobrazí skrytý form a aktivuje jej
        /// </summary>
        public void ShowForm()
        {
            Show();
            WindowState = FormWindowState.Normal;
            Activate();
        }

        private void contactListView_DoubleClick(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count == 0) return;

            Global.ISIMFunctions.OpenChatForm(((contactListView.SelectedItems[0]) as ContactListViewItem).contact, true);
        }

        /// <summary>
        /// Kontrola uživatelovi aktivity - automatické nastavení AFK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void availabilityTimer_Tick(object sender, EventArgs e)
        {
            if ((Global.UserActivity.IsInactive) && (availabilityCB.SelectedIndex == 0))
            {
                Global.awayAuto = true;
                availabilityCB.SelectedIndex = 1;
            }
            else if ((!Global.UserActivity.IsInactive) && (availabilityCB.SelectedIndex == 1) && (Global.awayAuto))
            {
                Global.awayAuto = false;
                availabilityCB.SelectedIndex = 0;
            }
        }

        #region !!HNUS!!
        private void checkConnectionTimer_Tick(object sender, EventArgs e)
        {
            if (fb != null)
            {
                Console.WriteLine("connection state fb: " + fb.xmppClient.XmppConnectionState);
                if (fb.xmppClient.XmppConnectionState != agsXMPP.XmppConnectionState.SessionStarted)
                {
                    fbDisconnect();
                    facebookThread = new Thread(new ThreadStart(facebookOpenConnection));
                    facebookThread.Name = "fb thread";
                    facebookThread.Start();
                }
            }
            else
            {
                Console.WriteLine("fb thread == null");
                facebookThread = new Thread(new ThreadStart(facebookOpenConnection));
                facebookThread.Name = "fb thread";
                facebookThread.Start();
            }

            if (gt != null)
            {
                Console.WriteLine("connection state gt: " + gt.xmppClient.XmppConnectionState);
                if (gt.xmppClient.XmppConnectionState != agsXMPP.XmppConnectionState.SessionStarted)
                {
                    gtDisconnect();
                    googleThread = new Thread(new ThreadStart(googleOpenConnection));
                    googleThread.Name = "gt thread";
                    googleThread.Start();
                }
            }
            else
            {
                Console.WriteLine("gt thread == null");
                googleThread = new Thread(new ThreadStart(googleOpenConnection));
                googleThread.Name = "fb thread";
                googleThread.Start();
            }

            if (icq != null)
            {
                Console.WriteLine("connection state icq: " + icq.session.IsConnected);
                if (!icq.session.IsConnected)
                {
                    icqDisconnect();
                    icqThread = new Thread(new ThreadStart(icqOpenConnection));
                    icqThread.Name = "icq thread";
                    icqThread.Start();
                }
            }
            else
            {
                Console.WriteLine("icq thread == null");
                icqThread = new Thread(new ThreadStart(icqOpenConnection));
                icqThread.Name = "icq thread";
                icqThread.Start();
            }

            if ((skype != null) && (skype.connectedToSkypeKit() == false))
            {
                skypeThread = new Thread(new ThreadStart(skypeOpenConnection));
                skypeThread.Name = "skype thread";
                skypeThread.Start();
            }
        }


        private void connected()
        {
            if (Global.disconnected == true)
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    checkConnectionTimer.Start();
                    availabilityTimer.Start();

                    if (availabilityCB.SelectedIndex == -1)
                        availabilityCB.SelectedIndex = 0;
                }));
                Global.disconnected = false;
            }
        }

        /// <summary>
        /// Připojí se ke skypekitu
        /// </summary>
        public void skypeOpenConnection()
        {
            if ((Login.Skype.username.Length > 0) && (skype == null))
            {
                skype = new SkypeClass();
                skype.connect();
                connected();
                Console.WriteLine("I'm connecting to skypekit");
            }
        }

        public void skypeDisconnect()
        {
            Console.WriteLine("skype disconnect");
            if (skype != null)
            {
                Console.WriteLine("skype disconnect not null");
                skype.disconnect();
                skype = null;
                if (skypeThread != null) skypeThread.Abort();
            }
        }


        public void facebookOpenConnection()
        {
            Console.WriteLine("fb open conenction");
            if ((Login.Facebook.userJid.Length > 0) && (fb == null))
            {
                fb = new XmppClass("facebook", Login.Facebook.userJid, Login.Facebook.password);
                fb.connect();
                connected();
                Console.WriteLine("connecting to fb");
            }
        }

        public void fbDisconnect()
        {
            Console.WriteLine("fb disconnect");
            if (fb != null)
            {
                Console.WriteLine("fb disconnect not null");
                fb.disconnect();
                fb = null;
                if (facebookThread != null) facebookThread.Abort();
            }
        }

        public void googleOpenConnection()
        {
            Console.WriteLine("gt open conenction");
            if ((Login.GoogleTalk.userJid.Length > 0) && (gt == null))
            {
                gt = new XmppClass("gtalk", Login.GoogleTalk.userJid, Login.GoogleTalk.password);
                gt.connect();
                connected();
                Console.WriteLine("connecting to gt");
            }
        }

        public void gtDisconnect()
        {
            Console.WriteLine("gt disconnect");
            if (gt != null)
            {
                Console.WriteLine("gt disconnect not null");
                gt.disconnect();
                gt = null;
                if (googleThread != null) googleThread.Abort();
            }
        }

        public void icqOpenConnection()
        {
            if ((Login.Icq.uin.Length > 0) && (icq == null))
            {
                icq = new IcqClass(Login.Icq.uin, Login.Icq.password);
                icq.connect();
                connected();
                Console.WriteLine("I'm connecting to icq");
            }
        }

        public void icqDisconnect()
        {
            if (icq != null)
            {
                icq.disconnect();
                icq = null;
                if (icqThread != null) icqThread.Abort();
            }
        }
#endregion;

        private void MainForm_Shown(object sender, EventArgs e)
        {
            if ((Login.Skype.username.Length > 0) || (Login.Facebook.userJid.Length > 0) || (Login.GoogleTalk.userJid.Length > 0) || (Login.Icq.uin.Length > 0))
            {
                skypeThread = new Thread(new ThreadStart(skypeOpenConnection));
                skypeThread.Name = "skype thread";
                facebookThread = new Thread(new ThreadStart(facebookOpenConnection));
                facebookThread.Name = "fb thread";
                googleThread = new Thread(new ThreadStart(googleOpenConnection));
                googleThread.Name = "gt thread";
                icqThread = new Thread(new ThreadStart(icqOpenConnection));
                icqThread.Name = "icq thread";

                skypeThread.Start();
                facebookThread.Start();
                googleThread.Start();
                icqThread.Start();

                Global.InternetConnection.CheckForUpdates();
            }
            else
            {
                ProtocolsForm protocolsForm = new ProtocolsForm();
                protocolsForm.ShowDialog();

                Global.OpenForms.preferencesForm = new PreferencesForm();
                Global.OpenForms.preferencesForm.ShowDialog();
            }
        }

        private void contactListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            if (e.ItemIndex == -1) return;

            ISIMContact contact = (contactListView.Items[e.ItemIndex] as ContactListViewItem).contact;

            using (Brush textBrush = new SolidBrush(Color.White))
            {
                Brush textColor = Brushes.Black;

                // rámeček
                if (((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected) || (e.Item.Selected == true))
                {
                    e.Graphics.FillRectangle(Brushes.MediumSeaGreen, e.Bounds);
                    e.Graphics.DrawRectangle(new Pen(Brushes.DarkGreen), e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
                    textColor = Brushes.White;
                }
                else if (contact.unreadedMessage == true)
                {
                    e.Graphics.FillRectangle(Brushes.Orange, e.Bounds);
                    e.Graphics.DrawRectangle(new Pen(Brushes.Red), e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
                }
                else if ((contact.isComposingMessage == true) || (contact.callStatus == CallStatus.isLive) || (contact.callStatus == CallStatus.ringingContact))
                {
                    e.Graphics.FillRectangle(Brushes.SkyBlue, e.Bounds);
                    e.Graphics.DrawRectangle(new Pen(Brushes.DodgerBlue), e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height - 1);
                }
                else
                    e.Graphics.FillRectangle(Brushes.White, e.Bounds);

                // avatar
                Bitmap contactAvatar = Global.Converter.GetContactAvatar(contact.id);
                if (contact.availability == Global.Availability.Offline)
                    contactAvatar = Global.ISIMFunctions.MakeImageGrayscale(contactAvatar);

                e.Graphics.DrawImage(contactAvatar, e.Bounds.Left + 1, e.Bounds.Top + 1, 26, 26);

                contactAvatar.Dispose();

                // dostupnost, píše, nepřečtená zpráva
                Bitmap availabilityImage;
                if (Global.settingsDictionary["bannedList"].ContainsKey(contact.id))
                    availabilityImage = Global.Converter.GetContactAvailabilityImage(Global.Availability.Blocked);
                else if (contact.unreadedMessage == true)
                    availabilityImage = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "messageIcon.png");
                else if (contact.isComposingMessage == true)
                    availabilityImage = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "composingIcon.png");
                else if ((contact.callStatus == CallStatus.isLive) || (contact.callStatus == CallStatus.ringingContact))
                    availabilityImage = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "callingIcon.png");
                else
                    availabilityImage = Global.Converter.GetContactAvailabilityImage(contact.availability);

                e.Graphics.DrawImage(availabilityImage, e.Bounds.Left + 5 + 25, e.Bounds.Top + 1, 12, 12);

                e.Graphics.DrawImage(Global.Converter.GetProtocolIcon(contact.protocol), e.Bounds.Left + 5 + 25, e.Bounds.Top + 1 + 13, 12, 12);

                e.Graphics.DrawString(Global.Converter.GetRenamedContactName(contact), e.Item.Font, textColor, e.Bounds.Left + 2 + 40, e.Bounds.Top + 1);
            }
        }

        private void contactListView_Resize(object sender, EventArgs e)
        {
            contactListView.Width = this.Width - 16;
            // aby se tile natáhnul po celé šíři
            contactListView.TileSize = new Size(this.Width - 37, contactListView.TileSize.Height);
        }

        /// <summary>
        /// Změna dostupnosti - stavu online/afk/dnd
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void availabilityCB_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Global.disconnected == true)
                return;

            Global.ISIMFunctions.ChangeMyAvailability(availabilityCB.SelectedIndex);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
            else
                CloseISIM();
        }

        private void CloseISIM()
        {
            if (Global.OpenForms.chatForm != null)
                Global.OpenForms.chatForm.Close();

            skypeDisconnect();
            fbDisconnect();
            gtDisconnect();
            icqDisconnect();

            Global.ChangeISIMsettings("mainFormWidth", this.Width.ToString());
            Global.ChangeISIMsettings("mainFormHeight", this.Height.ToString());

            Global.ChangeISIMsettings("mainFormPosX", this.Location.X.ToString());
            Global.ChangeISIMsettings("mainFormPosY", this.Location.Y.ToString());

            foreach (string list in Global.settingsDictionary.Keys)
                Global.ISIMFunctions.SaveISIMsettingsFile(list, Global.settingsDictionary[list]);

            foreach (string list in Global.userSpecialList.Keys)
                Global.ISIMFunctions.SaveISIMsettingsFile(list, Global.userSpecialList[list]);

            notifyIcon.Visible = false;
            notifyIcon.Dispose();

            Application.Exit();
        }

        private void protocolsLogin_Click(object sender, EventArgs e)
        {
            ProtocolsForm protocolsForm = new ProtocolsForm();
            protocolsForm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (Global.settingsDictionary["ISIMsettings"].ContainsKey("mainFormWidth"))
                    this.Width = int.Parse(Global.settingsDictionary["ISIMsettings"]["mainFormWidth"]);

                if (Global.settingsDictionary["ISIMsettings"].ContainsKey("mainFormHeight"))
                    this.Height = int.Parse(Global.settingsDictionary["ISIMsettings"]["mainFormHeight"]);
                else
                    this.Height = Screen.PrimaryScreen.WorkingArea.Height;

                if ((Global.settingsDictionary["ISIMsettings"].ContainsKey("mainFormPosX")) && (Global.settingsDictionary["ISIMsettings"].ContainsKey("mainFormPosY")))
                {
                    int x = int.Parse(Global.settingsDictionary["ISIMsettings"]["mainFormPosX"]);
                    int y = int.Parse(Global.settingsDictionary["ISIMsettings"]["mainFormPosY"]);

                    // ošetření vyskočení okna mimo obrazovku
                    if ((x >= Screen.PrimaryScreen.WorkingArea.Width - 5) || (x < 0 - this.Width + 5)) x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
                    if ((y >= Screen.PrimaryScreen.WorkingArea.Height - 5) || (y < 0 - this.Height + 5)) y = 0;

                    this.Location = new Point(x, y);
                }
                else
                {
                    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, 0);
                }
            }
            catch
            {
                Console.WriteLine("cannot set main form properties");
            }
        }

        private void preferencesItem_Click(object sender, EventArgs e)
        {
            if (Global.OpenForms.preferencesForm == null)
            {
                Global.OpenForms.preferencesForm = new PreferencesForm();
                Global.OpenForms.preferencesForm.StartPosition = FormStartPosition.Manual;
                Global.OpenForms.preferencesForm.Location = new Point(this.Location.X - 20, this.Location.Y + (this.Height / 4));
                Global.OpenForms.preferencesForm.Show();
            }
            else
                Global.OpenForms.preferencesForm.Activate();
        }

        private void removeContact_Click(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                if (MessageBox.Show(Global.LangManager.Translate("reallyRemoveContact") + ": " + Global.Converter.GetRenamedContactName(contact) + "?", Global.LangManager.Translate("removingContact"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                {
                    Global.ISIMFunctions.RemoveContact(contact);
                }
            }
        }

        private void contactMenu_Opening(object sender, CancelEventArgs e)
        {
            // získání indexu položky, imho to ale není potřeba a nikdy jsem to nepoužil, nechávám to tu jen tak jako poznámku
            //contactListView.Items.IndexOf(contactListView.GetItemAt(contactListView.PointToClient(MousePosition).X, contactListView.PointToClient(MousePosition).Y));

            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                remindWhenAvailable.Visible = contact.availability == Global.Availability.Offline;
                remindWhenAvailable.Checked = Global.userSpecialList["remindWhenAvailable"].Contains(contact.id);
                blockContact.Text = Global.settingsDictionary["bannedList"].ContainsKey(contact.id) ? Global.LangManager.Translate("unblock") : Global.LangManager.Translate("block");
                // zatím umí mazat kontakty jen skype
                removeContact.Visible = contact.protocol == "skype";
            }
        }

        private void blockContact_Click(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                if (!Global.settingsDictionary["bannedList"].ContainsKey(contact.id))
                {
                    string reason = "";
                    if (ShowDialogs.InputDialog.Show(Global.LangManager.Translate("blockContact"), Global.LangManager.Translate("enterReason"), ref reason) == System.Windows.Forms.DialogResult.OK)
                        Global.ISIMFunctions.BlockContact(contact, reason);
                }
                else
                    Global.ISIMFunctions.BlockContact(contact);
            }
        }

        private void remindWhenAvailable_Click(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                if (Global.userSpecialList["remindWhenAvailable"].Contains(contact.id))
                    Global.userSpecialList["remindWhenAvailable"].Remove(contact.id);
                else
                    Global.userSpecialList["remindWhenAvailable"].Add(contact.id);
            }
        }

        private void closeISIM_Click(object sender, EventArgs e)
        {
            CloseISIM();
        }

        private void showGuiItem_Click(object sender, EventArgs e)
        {
            ShowForm();
        }

        private void renameContact_Click(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                string newName = Global.Converter.GetRenamedContactName(contact);
                if (ShowDialogs.InputDialog.Show(Global.LangManager.Translate("renameContact"), Global.LangManager.Translate("enterContactNewName"), ref newName) == System.Windows.Forms.DialogResult.OK)
                    Global.ISIMFunctions.RenameContact(contact, newName);
            }
        }

        private void favouriteContact_Click(object sender, EventArgs e)
        {
            if (contactListView.SelectedItems.Count > 0)
            {
                ISIMContact contact = (contactListView.SelectedItems[0] as ContactListViewItem).contact;

                Global.ISIMFunctions.FavouriteContact(contact);
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
                ShowForm();
        }

        private void newSkypeContact_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms.OfType<SkypeContactSearch>().Any())
                Application.OpenForms.OfType<SkypeContactSearch>().First().Activate();
            else
            {
                SkypeContactSearch scs = new SkypeContactSearch(skype.GetSkypeInstance());
                scs.StartPosition = FormStartPosition.Manual;
                scs.Location = new Point(this.Location.X, this.Location.Y + (this.Height / 4));
                scs.Show();
            }
        }

        private void mainMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            newSkypeContact.Visible = skypeAccount.Visible = skype != null;
        }

        private void muteMic_Click(object sender, EventArgs e)
        {
            if (skype.MuteMic(!muteMic.Checked) == true)
                muteMic.Checked = !muteMic.Checked;
        }

        private void topMost_Click(object sender, EventArgs e)
        {
            this.TopMost = topMost.Checked;

            if (Global.OpenForms.chatForm != null)
                Global.OpenForms.chatForm.TopMost = this.TopMost;
        }

        private void aboutISIM_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms.OfType<AboutISIM>().Any())
                Application.OpenForms.OfType<AboutISIM>().First().Activate();
            else
            {
                AboutISIM aboutForm = new AboutISIM();
                aboutForm.Show();
            }
        }

        private void contactListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                contactListView_DoubleClick(null, null);
        }

        private void bugReportItem_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms.OfType<BugReport>().Any())
                Application.OpenForms.OfType<BugReport>().First().Activate();
            else
            {
                BugReport bugReport = new BugReport();
                bugReport.Show();
            }
        }

        private void skypeAccount_Click(object sender, EventArgs e)
        {
            if (Application.OpenForms.OfType<SkypeAccountPreferences>().Any())
                Application.OpenForms.OfType<SkypeAccountPreferences>().First().Activate();
            else
            {
                SkypeAccountPreferences  sap = new SkypeAccountPreferences(skype.GetAccountInstance());
                sap.Show();
            }
        }

        private void checkForUpdates_Click(object sender, EventArgs e)
        {
            Global.InternetConnection.CheckForUpdates(false);
        }  
    }
}
